Doodads = {}
local mt = {}
Doodads.__index = mt
setmetatable(Doodads, mt)



function Doodads.new(path)
	local self = setmetatable({}, Doodads)
	local file = io.open( path, 'rb' )
	self.file_string = file:read '*a'
	file:close()
	return self
end

function mt:load()
	--------------解析头部标志------------------
	local binary_reader = Binary_reader.new(self.file_string)

	--读4个字节固定长度的字符串
	local flag = binary_reader:read 'c4'
	if flag ~= 'W3do' then
		log.error('解析doo文件：该文件格式错误')
		return nil
	end
	--读32位的 一个有符号 long	uint32_t
	self.version = binary_reader:read 'l'
	if self.version ~= 8 then
		log.error('解析doo文件：doo版本错误，只支持1.24以上')
	end
	local unknow = binary_reader:read 'l'
	if unknow ~= 11 then
		log.error('解析doo文件：doo子版序号错误')
	end
	
	--------------解析id内容-------------------
	self.normal_doos = {}
	for i = 1, binary_reader:read 'l' do
		local obj = {}
		obj.id = binary_reader:read 'c4'
		--样式
		obj.variation = binary_reader:read 'l'
		obj.position = {
			binary_reader:read 'f',
			binary_reader:read 'f',
			binary_reader:read 'f',
		}
		--弧度制
		obj.angle = binary_reader:read 'f'
		obj.scale = {
			binary_reader:read 'f',
			binary_reader:read 'f',
			binary_reader:read 'f',
		}
		-- print(1, obj.id, obj.angle, obj.scale[1], obj.variation, obj.position[1], obj.position[2], obj.position[3])
		--可见性，I1:8位的int，可以试试能不能改成 b
		obj.state = binary_reader:read 'I1'
		obj.life = binary_reader:read 'I1'
		--掉落相关
		obj.drop_item_pointer = binary_reader:read 'l'
		obj.drop_item_set = {}
		for j = 1, binary_reader:read 'l' do
			self.drop_item_set[j] = {}
			for k = 1, binary_reader:read 'l' do
				self.drop_item_set[j][k] = {
					id = binary_reader:read 'c4',
					chance = binary_reader:read 'l'
				}
			end
		end

		obj.creation_number = binary_reader:read 'l'

		table.insert(self.normal_doos, obj)
	end

	--------------地形装饰物------------------
	--	悬崖/地形 分类
	self.special_format_version = binary_reader:read 'l'
	self.special_doos = {}
	for i = 1, binary_reader:read 'l' do
		local obj = {}
		obj.id = binary_reader:read 'c4'
		obj.variation = binary_reader:read 'l'
		obj.position = {
			binary_reader:read 'f',
			binary_reader:read 'f',
			0,
		}
		print(2, obj.id, obj.variation, obj.position[1], obj.position[2], obj.position[3])

		table.insert(self.special_doos, obj)
	end

	return true
end

function mt:each( type )
	local datas
	if type == '普通' then
		datas = self.normal_doos
	else
		datas = self.special_doos
	end

	local n = 0
	return function (t, v)
		n = n + 1
		return t[n]
	end, datas
end

function mt:save()

	local binary_writer = Binary_writer.new()
	binary_writer:write('c4', 'W3do')
	binary_writer:write('l', self.version)
	binary_writer:write('l', 11)

	--------------解析id内容-------------------
	binary_writer:write('l', #self.normal_doos)
	for i = 1, #self.normal_doos do
		local obj = self.normal_doos[i]
		--id
		binary_writer:write('c4', obj.id)
		--样式
		binary_writer:write('l', obj.variation)
		--位置
		binary_writer:write('f', obj.position[1])
		binary_writer:write('f', obj.position[2])
		binary_writer:write('f', obj.position[3])
		--角度：弧度
		binary_writer:write('f', obj.angle)
		--缩放
		binary_writer:write('f', obj.scale[1])
		binary_writer:write('f', obj.scale[2])
		binary_writer:write('f', obj.scale[3])
		--可见性
		binary_writer:write('I1', obj.state)
		binary_writer:write('I1', obj.life)
		--掉落相关
		binary_writer:write('l', obj.drop_item_pointer)
		binary_writer:write('l', #obj.drop_item_set)
		for j = 1, #obj.drop_item_set do
			local set = obj.drop_item_set[j]
			binary_writer:write('l', #set)
			for k = 1, #set do
				binary_writer:write('c4', set[k].id)
				binary_writer:write('l', set[k].chance)
			end
		end

		binary_writer:write('l', obj.creation_number)
	end

	--------------地形装饰物------------------
	binary_writer:write('l', self.special_format_version)
	binary_writer:write('l', #self.special_doos)
	for i = 1, #self.special_doos do
		local obj = self.special_doos[i]
		binary_writer:write('c4', obj.id)
		binary_writer:write('l', obj.variation)
		binary_writer:write('f', obj.position[1])
		binary_writer:write('f', obj.position[2])
		-- binary_writer:write('f', obj.position[3])
	end

	return binary_writer:file_write()
end